jQuery(document).ready(function() {
	var orgData = {};
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			orgData = data;
			$('#orgSelect').val(orgData.orgname);
			$('#orgSelect').attr('data', orgData.orgcode);
		}
	});
	
	bindOrg2("orgSelect");
	bindDict("businessType","DM_XKXMLX","");
	initTable();
	
	App.setPage("questions");  //Set current page
	App.init(); //Initialise plugins and elements

	//列表查询事件
	$('#search').click(function(){
		$('#questionsTable').bootstrapTable(('refresh')); 
	});
	
	// 新增常见问题
	$('#addQuestions').click(function(){
		setModalSize(0.6,0.6);
		$('#ifModal').attr("src", "add.html");
		$('#modalTile').html("常见问题-新增");
		$("#modal").modal('show');
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
});
//js函数
function initTable(){	
	
	$('#questionsTable').bootstrapTable({
		method: 'post',
	    url: projectPath + 'questions/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
            	pageNum: params.offset / params.limit + 1,
            	pageSize: params.limit,
            	title: $('#title').val(),
            	businessType: $('#businessType').val(),
            	orgCode: $('#orgSelect').attr('data')
            };
        },
	    columns: [{
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) { 
                var page = $('#questionsTable').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
	    }, {
	    	field: 'questionsId',
	    	visible: false
        }, {
	        field: 'title',
	        title: '标题',
	        width : '30%'
	    }, {
	        field: 'businessType',
	        visible: false
	    }, {
	     	field: 'businessTypeName',
	    	title: '业务类型'
	    }, {
	        field: 'orgName',
	        title: '所属机构'
	    }, {
	        field: 'orgCode',
	        visible: false
	    }, {
	        field: 'status',
	        title: '状态',
	        formatter:function(value,row,index){
	        	var result = value;
	        	if(value == "1"){
	        		result = '<span>已发布</span>';
	        	} else {
	        		result = '<span style="color:#ed5565" >未发布</span>';
	        	}
	        	return result;
	        }
	    }, {
	        field: 'publishDate',
	        title: '发布时间'
	    }, {
			field : 'operate',
			title : '操作',
			align : 'center',
			width : '230px',
			events : operateEvents,
			formatter : operateFormatter
		}]
	});
}

function operateFormatter(value, row, index) {
	return [
	        '<button type="button" class="edit btn btn-info btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
	        '<button type="button" class="delete btn btn-danger btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>',
	        ].join('');
//	if(row.status == '1'){
//	} else {
//		return [
//		        '<button type="button" class="edit btn btn-info btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
//		        '<button type="button" class="delete btn btn-danger btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>',
//		        ].join('');
//	}
}

window.operateEvents = {
	'click .edit': function (e, value, row, index) {
		$("#save").css("display","inline");
		$('#modalTile').html("常见问题-编辑");
		if(row.state == '1'){
			$("#save").css("display","none");
			$('#modalTile').html("常见问题-预览");
		}

		setModalSize(0.6,0.6);
		
    	var questionsId = row.questionsId;
    	$('#ifModal').attr("src", "edit.html?questionsId=" + questionsId);
    	
		$("#modal").modal('show');
    },
    'click .delete': function (e, value, row, index) {
    	var questionsId = row.questionsId;
		var title = row.title;
		window.wxc.xcConfirm('是否<strong>删除</strong>常见问题数据?<br /><span style="color:red;">['+ title + ']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({
					url : projectPath + 'questions/delete.do',
					data : {
						"questionsId" : questionsId
					},
					dataType : "json",
					success : function(data) {
						if (data.result == 1) {
							$('#questionsTable').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
						}
					}
				});
			}
		});
    }
};

function setModalSize(widthPercent, heightPercent){
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#modalW').width(documentW * widthPercent);
	$('#modalH').height(documentH * heightPercent);
}


//获取一行数据
function getQuestionsDataById(questionsId){
	var datas = $('#questionsTable').bootstrapTable('getData');  	
	for(var i = 0; i<datas.length; i++){
		var data = datas[i];
		if(questionsId == data.questionsId){
			return data;
		}
	}
	return null;
}